//
//  AppMainVC.swift
//  MIFI
//
//  Created by 金嗓子卡卡 on 2019/4/16.
//  Copyright © 2019 NJMifi. All rights reserved.
//

import UIKit

class AppMainVC: RootVC {

    private var datas = [
//                         ("Share File","ShareFileVC"),
                         ("SMS","SMSInfoVC"),
                         ("USSD","USSDInfoVC")]
//                         ("Recharge","RechargeVC")]
    
    private lazy var tableView: UITableView = {
        let tab = UITableView(frame: CGRect(x: 0, y: naviView!.bottom, width: screenWidth, height: screenHeight - tabbarHeight - naviView!.bottom), style: .grouped)
        tab.backgroundColor = themeColor
        tab.rowHeight = 50
        tab.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tab.delegate = self
        tab.dataSource = self
        tab.separatorStyle = .none
        return tab
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(loginOutChangeToTop), name: NSNotification.Name(rawValue: "deviceDisconnect"), object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(getDataRefreshSMS), name: NSNotification.Name(rawValue: "getDataRefreshSMS"), object: nil)
        
//          NotificationCenter.default.post(name: NSNotification.Name(rawValue: ""), object: model)
    
    
}

    @objc func getDataRefreshSMS(notification:NSNotification){
        
        
  
        let dict = notification.object as! StatusVCDeviceInfoModel
        
        
        
        if dict.sms_unread_num != nil {
            
            if dict.sms_unread_num == "0" || dict.sms_unread_num!.isEmpty   {
                
                datas = [
                    ("SMS","SMSInfoVC"),
                    ("USSD","USSDInfoVC")]
                
            }else{
                datas = [
                    ("SMS (" + dict.sms_unread_num! + " unread )","SMSInfoVC"),
                    ("USSD","USSDInfoVC")]
            }
           
            
            
          self.tableView.reloadData()
        }
        
        
        
        
        
    }

@objc  private func loginOutChangeToTop(){
    
    
    if !UserInfoManager.shared().isConnected {
        
        
        if (self.navigationController?.viewControllers.count)! > 1 {
            
            self.navigationController?.popToRootViewController(animated: true)
            
        }
        
    }
    
}
    override func configView() {
        configNaviView(title: "App")
        leftBtn?.isHidden = true
        textLabel?.font = UIFont.systemFont(ofSize: 20)
        
        bottomLine?.isHidden = false
        bottomLine?.frame = CGRect(x: 12, y: naviView!.height - 1, width: naviView!.width - 12 * 2, height: 1)
        bottomLine?.backgroundColor = whiteColor
        
        view.addSubview(tableView)
    }
    
}

extension AppMainVC : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        
        cell.backgroundColor = clearColor
        cell.contentView.backgroundColor = clearColor
        
        if cell.contentView.layer.sublayers != nil {
            for layer in cell.contentView.layer.sublayers! {
                layer.removeFromSuperlayer()
            }
        }
        
        cell.textLabel?.text = datas[indexPath.row].0
        cell.textLabel?.backgroundColor = clearColor
        cell.textLabel?.textColor = whiteColor
        cell.textLabel?.font = UIFont.systemFont(ofSize: 18)
        
        let line = CALayer()
        line.backgroundColor = whiteColor.cgColor
        line.frame = CGRect(x: 12, y: 49, width: tableView.width - 12 * 2, height: 1)
        cell.contentView.layer.addSublayer(line)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        cell?.setSelected(false, animated: false)
        checkLogin {[weak self] in
            self?.gotoNewVC(vcname: (self?.datas[indexPath.row].1)!)
            
        
            
        }
//        gotoNewVC(vcname: datas[indexPath.row].1)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
